﻿#ifndef UIMODELTREEVIEW_H
#define UIMODELTREEVIEW_H

#include <QMap>
#include <QSharedPointer>
#include <QTreeView>
#include <QStandardItemModel>
#include <QStyledItemDelegate>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QMessageBox>

#include <OpenXLSX.hpp>

#include <ElaTreeView.h>

#include <devicemodel/CModelInfo.h>

using namespace OpenXLSX;

class UiModelTreeItemDelegate : public QStyledItemDelegate{
    Q_OBJECT
public:
    enum ESizeModel {
        MiniSize = 0x00,
        AutoSize,
        MaxSize
    };

    explicit UiModelTreeItemDelegate(QTreeView *treeView, QObject *parent = nullptr);
protected:
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
    bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);


private:
    QTreeView *m_treeView = nullptr;

    int m_modeWidth;
    QColor m_backGroundColor;
    QSize m_expandIconSize;

    QPixmap m_expandIcon;
    QPixmap m_foldIcon;

    int m_rootNodeHeight = 50;
    int m_rootSpacing = 6;
    QColor m_rootHoaver;
    QColor m_rootNoHoaver;
    QColor m_rootIconColor;
    QColor m_rootTextColor;
    QPixmap m_rootIcon;
    QFont m_rootFont;

    int m_oneNodeHeight = 40;
    QColor m_oneHoaver;
    QColor m_oneNoHoaver;
    QColor m_oneIconColor;
    QPixmap m_oneIcon;
    bool m_oneIconDir;
    QFont m_oneFont;

    mutable ESizeModel m_mode;
};

class IModel;
class UiModelTreeView : public ElaTreeView
{
    Q_OBJECT
public:
    enum ENodeValueRole {
        Model = Qt::UserRole,
        NodelLevel = Qt::UserRole+1,
        NodeIcon = Qt::UserRole+2,
        NodeTopAndEndFlag = Qt::UserRole+3,
    };

    enum ENodeTopEndFlag {
        OtherNode = 0x00,
        TopNode,
        EndNode
    };

    enum ENodelLevel {
        Root = 0x00,
        Node_Level_1
    };

    explicit UiModelTreeView(QWidget *parent = nullptr);

signals:
    void sigSelectModelChange(QSharedPointer<CModelInfo>& model);

public slots:
    void reload();

    void onAddModel();

    void onImportSig(QString finePath);

protected:
    QJsonArray sheetToJson(XLWorksheet& yxSheet);
    void viewAddModel(QSharedPointer<CModelInfo>& model);


private:
    QMap<QString, QSharedPointer<CModelInfo>> m_models;
    QStandardItemModel* m_viewModel;
};

#endif // UIMODELTREEVIEW_H
